<?php

class CProduct extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;
	
	public function _setup(){
		
		$this->_name=R_PRE."products";
		
		$this->_primary="productid";
		
		parent::_setup();
		
	}
	
	public function createProduct($data){
		
		$productid=$this->insert($data);
		
		return $productid;
		
	}
	
	
	public function editProduct($itemid,$data){
		
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('itemid = ?', $itemid);

		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
		
	}
	/**
	 * 批量更改产品尺寸
	 * @param array $itemid
	 * @param int $size
	 */
	public function editProductSize($itemid,$pattern_size,$pattern_size_stock){
		$db=$this->getAdapter();
		$pattern_size = serialize($pattern_size);
		$pattern_size_stock = serialize($pattern_size_stock);
		$sql ="update zcshop_products 
			set product_pattern='$pattern_size',product_pattern_stock='$pattern_size_stock' 
			where itemid in ($itemid)";
		$rows_affected = $db->query($sql);
		if($rows_affected===false){
			return false;
		}else{
			return true;
		}
	}
	
	public function getDetail($itemid){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
		
		$select->where('itemid = ? ',$itemid);
		
		return $db->fetchAll($select);
		
	}
	public function getAllProduct($productid){
		$db=$this->getAdapter();
		$select = $db->select();
		$select->from('zcshop_products', '*');
		$select->where("zcshop_products.productid in ($productid)");
		$select->join('zcshop_items', 'zcshop_items.itemid = zcshop_products.itemid', '*')->join('zcshop_product_price', 'zcshop_product_price.itemid = zcshop_products.itemid', '*');
		$select->group("zcshop_products.productid");
		$select .=" ORDER BY INSTR(',$productid,',CONCAT(',',zcshop_products.productid,','))";
		$result=$db->fetchAll($select);
		return $result;
	}
	
	public function delProducts($itemidArray){
		
		$db=$this->getAdapter();
		
		$itemidString=join(',',$itemidArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE itemid IN ($itemidString)");
		
		$couponcode = new CCouponCode();
		$couponcode->productdel($itemidArray);
		return $result;
		
	}
	
	public function getPatternPrice(){
		
		
		
	}
	
	public function getProductByFlidCounts($flidArray){
		
		$db=$this->getAdapter();
		
		$flidString=join(',',$flidArray);
		
		$itemTable=R_PRE."items";
		
		$result=$db->fetchOne("SELECT COUNT(*) FROM $itemTable  
						WHERE item_type='product' 
						AND flid IN($flidString)");
						
		return $result;
		
	}
	
	public function getProductsByFlid($flidArray,$from,$counts,$order=null){
		
		if($order==null){
			
			$order="A.itemid DESC";
			
		}
		
		$db=$this->getAdapter();
		
		$flidString=join(',',$flidArray);
		
		$productTable=$this->_name;
		
		$itemTable=R_PRE."items";
		
		$priceTable=R_PRE."product_price";
		
		$userTable=R_PRE."members";
		
		$result=$db->fetchAll("
							SELECT MIN($priceTable.net_price) AS min_price,MAX($priceTable.net_price) AS max_price,A.*,U.uid,U.username  
							FROM $priceTable,(
								SELECT $productTable.pattern_intro,$productTable.cut_url,$productTable.valid_days,$productTable.wholesale,$productTable.free_shipping,$itemTable.*
								FROM $productTable,$itemTable 
								WHERE $productTable.itemid=$itemTable.itemid 
								AND flid IN ($flidString) 
								)AS A
							LEFT JOIN $userTable AS U ON U.uid=A.uid 
							WHERE A.itemid=$priceTable.itemid 
							GROUP BY $priceTable.itemid 
							ORDER BY $order 
							LIMIT $from,$counts");
		return $result;				
		
	}
	
	public function getOverdueProducts($from,$counts,$uid=0){
		
		$db=$this->getAdapter();
		
		$where='';
		
		if($uid!=0){
			
			$where="AND ".R_PRE."items.uid='$uid'";
			
		}
		
		$result=$db->fetchAll("SELECT (valid_days*24*3600+add_time-UNIX_TIMESTAMP()) AS delay,".R_PRE."items.* 
							FROM ".R_PRE."products,".R_PRE."items 
							WHERE ".R_PRE."products.itemid=".R_PRE."items.itemid 
							$where 
							ORDER BY delay ASC 
							LIMIT $from,$counts 
							");
							
		return $result;
		
	}
	
	public function editProducts($itemidArray,$data){
		
		$db=$this->getAdapter();
		
		$itemidString=join(',',$itemidArray);
		
		$where = "itemid IN ($itemidString)";

		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected>0){
			
			return false;
			
		}else{
			
			return true;
			
		}
		
	}
	
	public function updateStock($count,$itemid)
	{
		$db=$this->getAdapter();
		$rows_affected = $db->query("update $this->_name set stock=stock{$count} where itemid=$itemid");
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
	}
	
	public function getPatternStock($size, $count, $itemid, $sep = 'sub')
	{		
		$product = $this->fetchRow(array('itemid = ?'=>$itemid));
		$patternStock = is_array(unserialize($product->product_pattern_stock)) ? unserialize($product->product_pattern_stock) : array();
		$stock = 0;				
		foreach ($patternStock as $k=>$v) {
			$k = trim($k);
			$size = trim($size);			
			if ($k == $size) {
				if ($sep == 'sub') {
					$v = $v - $count;					
				} else {
					$v = $v + $count;
				}
				$stock = $v;
			}
		}
		if ($product->sku) {
			$skuArr = CStock::getStock($product->sku);
			if ($skuArr) {
				$i = 0;
				foreach ($skuArr['size'] as $v) {
					if ($v == $size) {
						$stock = $skuArr['stock'][$i];
						$stock -= $count;
					}
					$i++;
				}
			}
		}
		return $stock;
	}
	
	public function updatePatternStock($size, $count, $itemid, $sep = 'sub')
	{		
		$product = $this->fetchRow(array('itemid = ?'=>$itemid));
		$patternStock = is_array(unserialize($product->product_pattern_stock)) ? unserialize($product->product_pattern_stock) : array();
		$stock = 0;				
		foreach ($patternStock as $k=>&$v) {
			$k = trim($k);
			$size = trim($size);			
			if ($k == $size) {
				if ($sep == 'sub') {
					$v = $v - $count;					
				} else {
					$v = $v + $count;
				}
				$stock = $v;
			}
		}		
		$db = $this->getAdapter();
		$where = $db->quoteInto('itemid = ?', $itemid);
		$this->update(array('product_pattern_stock'=>serialize($patternStock)), $where);
		return $stock;
	}
	
	public function updateSalesStatus($status,$itemids)
	{
		$db=$this->getAdapter();
		$rows_affected = $db->query("update $this->_name set onsales={$status} where itemid in ({$itemids})");
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
	}
	public function updatePresalesStatus($itemid,$status)
	{
		$db=$this->getAdapter();
		$rows_affected = $db->query("update $this->_name set ispresale={$status} where itemid=$itemid");
		if($rows_affected!=1){
				
			return false;
				
		}else{
				
			return true;
				
		}
	}
	
	public function copy($itemid = null)
	{
		if($itemid == null)
			return false;
		//copy,include{attachments,price,item,product}
		$newitem = array();
		$newitemid = 0;
		$curl_url = '';
		$newproductdetail = array();
		$newproductid = 0;
		$pricedata = array();
		$attachments = array();
		
		try{
			//item
			$item = new CItem();
			$newitem = $item->getOneItem($itemid,'product');
			$newitem = $newitem[0];
			unset($newitem['itemid'],$newitem['update_time']);
			$newitem['add_time'] = time();
			$newitemid = $item->createItem($newitem);
			$change_curl_url = false;
			
			if($newitemid > 0)
			{
				//product
				$newproductdetail = $this->getDetail($itemid);
				$newproductdetail = $newproductdetail[0];
				unset($newproductdetail['productid']);
				$curl_url = $newproductdetail['cut_url'];
				$newproductdetail['itemid'] = $newitemid;
				$newproductdetail['is_showed'] = 1;//unshow
				$newproductid = $this->createProduct($newproductdetail);
				
				//price
				$price = new CProductPrice();
				$pricedata = $price->getAllPrice($itemid);
				foreach ($pricedata as $pricek=>$pricev) {
					$pricedata[$pricek]['itemid'] = $newitemid;
					unset($pricedata[$pricek]['priceid']);
					$price->createPrice($pricedata[$pricek]);
				}
				
				//attachments
				$attachment = new CAttachment();
				$path=new CPath();
				$attachments = $attachment->getAttachments($itemid);
				for ($i = 0; $i < count($attachments); $i++)
				{
					$newPath_url = '';
					$newPath_cut_url = '';
					$newPath_url = $path->toDbPath('item',preg_replace('/(\w+\.)/', $i.time().CString::randstr(8).'.', $attachments[$i]['attachment_url']));
            		CFile::copyFile($path->toAbsoluteRootPath($attachments[$i]['attachment_url']),$path->toAbsoluteRootPath($newPath_url));
            		
            		if (!empty($attachments[$i]['cut_url'])) {
	            		$newPath_cut_url = $path->toDbPath('cut_item',preg_replace('/(\w+\.)/', $i.time().CString::randstr(8).'.', $attachments[$i]['cut_url']));
	            		CFile::copyFile($path->toAbsoluteRootPath($attachments[$i]['cut_url']),$path->toAbsoluteRootPath($newPath_cut_url));
            		}
            		if($attachments[$i]['cut_url'] == $curl_url) {
            			$curl_url = $newPath_cut_url;
            			$orgUrl = $attachments[$i]['attachment_url'];
            			$change_curl_url = true;
            		}
            		
            		$attachments[$i]['attachment_url'] = $newPath_url;
            		$attachments[$i]['cut_url'] = $newPath_cut_url;
            		$attachments[$i]['itemid'] = $newitemid;
            		unset($attachments[$i]['attachmentid']);
				}
				$attachment->createAttachments($attachments);
				
				if($change_curl_url) {
					$img=new CClsImage();
                	$toPath = str_replace('item_img', 'medium_img',$orgUrl);
	        		$toPathArr = pathinfo($toPath);
	        		$fileName = $toPathArr['filename'] . rand(1,100);
	        		$img->make_thumb2(MY_ROOT . "/" .$orgUrl,260,260,MY_ROOT . "/" . $toPathArr['dirname'] . '/', '', $fileName);
	        		$medium_url = $toPathArr['dirname'] . '/' . $fileName . '.jpg';
					$this->editProduct($newitemid, array('cut_url'=>$curl_url, 'medium_url'=>$medium_url));
				}
				
			}
			else {
				throw new Exception('生成条目失败');
			}
			
		} catch (Exception $e)
		{
			return false;
		}
		return $newitemid;
	}
	
	public function copytosite($itemid = null,$site = null)
	{
		if($site == null)
			return false;
		$db_adapter = null;
		$root = null;
		switch ($site) {
			case 'goaljerseys':
				$db_adapter = Zend_Db::factory('PDO_MYSQL',
						array(
							'host'				=> 'localhost',
		                    'username'          => 'admin',
		                    'password'          => 'qa1234',
		                    'dbname'            => 'goaljerseys',
		        ));
		        $root = 'goaljerseys.com';
		        break;			
		}
		if($db_adapter == null)
			return false;
		if($itemid == null)
			return false;
		//copy,include{attachments,price,item,product}
		$newitem = array();
		$newitemid = 0;
		$curl_url = '';
		$newproductdetail = array();
		$newproductid = 0;
		$pricedata = array();
		$attachments = array();
		
		try{
			//item
			$item = new CItem();
			$newitem = $item->getOneItem($itemid,'product');
			$newitem = $newitem[0];			
			$newitem['fromitemid'] = $itemid;
			unset($newitem['itemid'],$newitem['update_time']);
			
			$newitem['uid'] = 1;
			$newitem['add_time'] = time();
			//$newitem['flid'] = 0;			
			$db_adapter->insert(R_PRE.'items',$newitem);
			$newitemid = $db_adapter->lastInsertId();
			$change_curl_url = false;
			
			if($newitemid > 0)
			{
				//product
				$newproductdetail = $this->getDetail($itemid);
				$newproductdetail = $newproductdetail[0];
				unset($newproductdetail['productid']);				
				$curl_url = $newproductdetail['cut_url'];
				$newproductdetail['itemid'] = $newitemid;
				$newproductdetail['is_showed'] = 0;//unshow
				$db_adapter->insert(R_PRE.'products',$newproductdetail);
				$newproductid = $db_adapter->lastInsertId();
				
				//price
				$price = new CProductPrice();
				$pricedata = $price->getAllPrice($itemid);
				foreach ($pricedata as $pricek=>$pricev) {
					$pricedata[$pricek]['itemid'] = $newitemid;
					unset($pricedata[$pricek]['priceid']);
					$db_adapter->insert(R_PRE.'product_price',$pricedata[$pricek]);
				}
				
				//attachments
				$attachment = new CAttachment();
				$path=new CPath();
				$attachments = $attachment->getAttachments($itemid);
				for ($i = 0; $i < count($attachments); $i++)
				{
					$newPath_cut_url = '';
					$newPath_url = '';
					$pathPre = "/data/www/goaljerseys/";
					
					if (!empty($attachments[$i]['cut_url'])) {
						$newPath_url = $path->toDbPath('item',preg_replace('/(\w+\.)/', $i.time().CString::randstr(8).'.', $attachments[$i]['attachment_url']));
						CFile::copyFile($path->toAbsoluteRootPath($attachments[$i]['attachment_url']),$pathPre . $newPath_url);	            		
	            		$newPath_cut_url = $path->toDbPath('cut_item',preg_replace('/(\w+\.)/', $i.time().CString::randstr(8).'.', $attachments[$i]['cut_url']));
	            		CFile::copyFile($path->toAbsoluteRootPath($attachments[$i]['cut_url']),$pathPre . $newPath_cut_url);
            		} else {
            			$newPath_url =  $attachments[$i]['attachment_url'];
		            	CFile::copyFile($path->toAbsoluteRootPath($attachments[$i]['attachment_url']),$pathPre . $newPath_url);
            		}
            		
            		if($attachments[$i]['cut_url'] == $curl_url) {
            			$curl_url = $newPath_cut_url;
            			$toPath = str_replace('item_img', 'medium_img', $attachments[$i]['attachment_url']);
            			$toPathArr = pathinfo($toPath);
        
            			$img=new CClsImage();
        				$filename = $img->make_thumb2(MY_ROOT . "/" .$attachments[$i]['attachment_url'],260,260, $pathPre . $toPathArr['dirname'] . '/', '', $toPathArr['filename']);
            			$medium_url = $toPathArr['dirname'] . '/' . $toPathArr['filename'] . '.jpg';
            			$change_curl_url = true;
            			
            			$toPath = str_replace('cut_item_img', 'new_cut_item_img', $curl_url);
        				$toPathArr = pathinfo($toPath);
        				
        				$filename = $img->make_thumb2(MY_ROOT . "/" .$attachments[$i]['cut_url'],122,0,$pathPre . "/" . $toPathArr['dirname'] . '/', '', $toPathArr['filename']);
            		}
            		
            		$attachments[$i]['attachment_url'] = $newPath_url;
            		$attachments[$i]['cut_url'] = $newPath_cut_url;
            		$attachments[$i]['itemid'] = $newitemid;
            		unset($attachments[$i]['attachmentid']);
            		$db_adapter->insert(R_PRE.'attachments',$attachments[$i]);
				}
				
				if($change_curl_url)
					$db_adapter->update (R_PRE.'products',
                            array ('cut_url'        => $curl_url,'medium_url'=>$medium_url),
                            $this->_db->quoteInto ('itemid=?', $newitemid));
                            
				$badgeArray = $this->getAdapter()->fetchAll('select * from zcshop_badge_product where itemid = ' . intval($itemid));
				$db_adapter->query('delete from zcshop_badge_product where itemid = ' . intval($newitemid));
				foreach ($badgeArray as $v) {
	        		$db_adapter->query('insert into zcshop_badge_product (itemid, badge_id) values(' . intval($newitemid) . ', ' . intval($v['badge_id']) . ')');
	        	}
				
			}
			else {
				throw new Exception('生成条目失败');
			}
			
		} catch (Exception $e)
		{
			echo $e->getMessage();
			return false;
		}
		return $newitemid;
	}
	
	public function edittosite($itemid = null,$site = null)
	{
		if($site == null)
			return false;
		$db_adapter = null;
		$root = null;
		switch ($site) {
			case 'goaljerseys':
				$db_adapter = Zend_Db::factory('PDO_MYSQL',
						array(
							'host'				=> 'localhost',
		                    'username'          => 'admin',
		                    'password'          => 'qa1234',
		                    'dbname'            => 'goaljerseys',
		        ));
		        $root = 'goaljerseys.com';
		        break;		
		}
		if($db_adapter == null)
			return false;
		if($itemid == null)
			return false;
		//copy,include{attachments,price,item,product}
		$newitem = array();
		$newitemid = 0;
		$curl_url = '';
		$newproductdetail = array();
		$newproductid = 0;
		$pricedata = array();
		$attachments = array();
		
		try{
			//item
			$item = new CItem();
			$newitem = $item->getOneItem($itemid,'product');
			$newitem = $newitem[0];			
			unset($newitem['itemid'],$newitem['update_time']);//
			
			$newitem['uid'] = 1;
			//unset($newitem['flid']);
			$where = 'fromitemid = ' . $itemid;
			
			$db_adapter->update(R_PRE.'items',$newitem, $where);
			
			$change_curl_url = false;
			
			$newItemIdArray = $db_adapter->fetchAll('select * from zcshop_items where fromitemid=' . $itemid);
			if (empty($newItemIdArray[0])) {
				return false;
			}
			$newitemid = $newItemIdArray[0]['itemid'];
			
			//product
			$newproductdetail = $this->getDetail($itemid);
			$newproductdetail = $newproductdetail[0];
			unset($newproductdetail['productid']);
			
			$curl_url = $newproductdetail['cut_url'];
			$newproductdetail['itemid'] = $newitemid;
			//$newproductdetail['is_showed'] = 0;//unshow
			$db_adapter->update(R_PRE.'products',$newproductdetail, 'itemid=' . $newitemid);
			$newproductidArray =$db_adapter->fetchAll('select * from zcshop_products where itemid=' . $newitemid);
			$newproductid = $newproductidArray[0]['productid'];
			//price
			$price = new CProductPrice();
			$pricedata = $price->getAllPrice($itemid);
			$i = 0;
			foreach ($pricedata as $pricek=>$pricev) {
				$pricedata[$pricek]['itemid'] = $newitemid;
				unset($pricedata[$pricek]['priceid']);
				if ($i == 0) {
					$db_adapter->delete(R_PRE.'product_price','itemid=' . $newitemid);
					$i++;
				}
				$db_adapter->insert(R_PRE.'product_price',$pricedata[$pricek]);
			}
			
			//attachments
			$attachment = new CAttachment();
			$path=new CPath();
			$attachments = $attachment->getAttachments($itemid);
			$j = 0;
			for ($i = 0; $i < count($attachments); $i++)
			{
				$newPath_cut_url = '';
				$newPath_url = '';
				$pathPre = "/data/www/goaljerseys/";
				if (!empty($attachments[$i]['cut_url'])) { //普通图片
					$newPath_url = $path->toDbPath('item',preg_replace('/(\w+\.)/', $i.time().CString::randstr(8).'.', $attachments[$i]['attachment_url']));
		            CFile::copyFile($path->toAbsoluteRootPath($attachments[$i]['attachment_url']),$pathPre . $newPath_url);
	            	$newPath_cut_url = $path->toDbPath('cut_item',preg_replace('/(\w+\.)/', $i.time().CString::randstr(8).'.', $attachments[$i]['cut_url']));
	            	CFile::copyFile($path->toAbsoluteRootPath($attachments[$i]['cut_url']),$pathPre . $newPath_cut_url);
	            } else { //描述图片
	            	$newPath_url =  $attachments[$i]['attachment_url'];
		            CFile::copyFile($path->toAbsoluteRootPath($attachments[$i]['attachment_url']),$pathPre . $newPath_url);
	            }
	            
	            if($attachments[$i]['cut_url'] == $curl_url) {
	            	$curl_url = $newPath_cut_url;
	            	$change_curl_url = true;
	            	$toPath = str_replace('item_img', 'medium_img', $attachments[$i]['attachment_url']);
            		$toPathArr = pathinfo($toPath);
            		$img=new CClsImage();
        			$filename = $img->make_thumb2(MY_ROOT . "/" .$attachments[$i]['attachment_url'],260,260, $pathPre . $toPathArr['dirname'] . '/', '', $toPathArr['filename']);
            		$medium_url = $toPathArr['dirname'] . '/' . $toPathArr['filename'] . '.jpg';
            		
            		$toPath = str_replace('cut_item_img', 'new_cut_item_img', $curl_url);
        			$toPathArr = pathinfo($toPath);
        			$filename = $img->make_thumb2(MY_ROOT . "/" .$attachments[$i]['cut_url'],122,0,$pathPre . "/" . $toPathArr['dirname'] . '/', '', $toPathArr['filename']);
	            }
	            
	            $attachments[$i]['attachment_url'] = $newPath_url;
	            $attachments[$i]['cut_url'] = $newPath_cut_url;
	            $attachments[$i]['itemid'] = $newitemid;
	            unset($attachments[$i]['attachmentid']);	          
	            if ($j == 0) {
	           		$db_adapter->delete(R_PRE.'attachments','itemid=' . $newitemid);
	           		$j++;
	            }	           
				$db_adapter->insert(R_PRE.'attachments',$attachments[$i]);	           
			}
			
			if($change_curl_url)
				$db_adapter->update (R_PRE.'products',
	             					array ('cut_url'        => $curl_url, 'medium_url'=>$medium_url),
	                           		 $this->_db->quoteInto ('itemid=?', $newitemid));
				
			//臂章
			$badgeArray = $this->getAdapter()->fetchAll('select * from zcshop_badge_product where itemid = ' . intval($itemid));
			$db_adapter->query('delete from zcshop_badge_product where itemid = ' . intval($newitemid));
			foreach ($badgeArray as $v) {
        		$db_adapter->query('insert into zcshop_badge_product (itemid, badge_id) values(' . intval($newitemid) . ', ' . intval($v['badge_id']) . ')');
        	}
        
			
		} catch (Exception $e)
		{
			echo $e->getMessage();
			return false;
		}
		return $newitemid;
	}
	
	public function copyIsShow($itemid, $site, $show)
	{
		if($site == null)
			return false;
		$db_adapter = null;
		switch ($site) {
			case 'goaljerseys':
				$db_adapter = Zend_Db::factory('PDO_MYSQL',
						array(
							'host'				=> 'localhost',
		                    'username'          => 'admin',
		                    'password'          => 'qa1234',
		                    'dbname'            => 'goaljerseys',
		        ));		      
		        break;		
		}
		if($db_adapter == null)
			return false;
		if($itemid == null)
			return false;
		$row = $db_adapter->fetchAll('select * from zcshop_items where fromitemid=' . intval($itemid));
		if(empty($row[0])) {
			return;
		}
	
		$newitemid = $row[0]['itemid'];
		return $db_adapter->update(R_PRE.'products', array('is_showed'=>intval($show)), 'itemid=' . $newitemid);
	}
	
	public function copyAttr($itemid, $site)
	{
		if($site == null)
			return false;
		$db_adapter = null;
		switch ($site) {
			case 'goaljerseys':
				$db_adapter = Zend_Db::factory('PDO_MYSQL',
						array(
							'host'				=> 'localhost',
		                    'username'          => 'admin',
		                    'password'          => 'qa1234',
		                    'dbname'            => 'goaljerseys',
		        ));		      
		        break;		
		}
		if($db_adapter == null)
			return false;
		if($itemid == null)
			return false;
		$localAttr = new CProductattr();
		$rows = $localAttr->fetchAll(array('itemid = ?'=>$itemid));
	
		$row = $db_adapter->fetchAll('select * from zcshop_items where fromitemid=' . intval($itemid));
		if(empty($row[0])) {
			return;
		}
	
		$newitemid = $row[0]['itemid'];
		$db_adapter->query('delete from zcshop_product_attr where itemid=' . $newitemid);
		foreach ($rows as $v) {
			$db_adapter->insert('zcshop_product_attr', array('itemid'=>$newitemid, 'attr_name'=>$v->attr_name, 'attr_value'=>$v->attr_value, 'attr_id_ex'=>$v->attr_id_ex));
		}
	}
	
	public function getTopProduct($cid = 0, $from = 0, $limit = 0)
	{
		$item = new CItem();
		$where = R_PRE . "product_attr.attr_name='isTop' and " . R_PRE . "product_attr.attr_value = '1' and onsales=0 and is_showed=1";
			
		$itemArray = $item->getAllItems($from,$limit,'product',$cid,0,'has_banned',$where,'px ASC,add_time DESC');
		return $itemArray;
	}
	
	public function getAllAttr($itemid)
	{
		$attr = new CProductattr();
		return $attr->fetchAll(array('itemid = ?'=>$itemid));
	}
	/**
	 * 获取预售产品
	 */
	public function getPreProduct(){
		$db=$this->getAdapter();
		$select = $db->select();
		$select->from('zcshop_products', '*');
		$select->where("zcshop_products.ispresale=1");
		$select->join('zcshop_items', 'zcshop_items.itemid = zcshop_products.itemid', '*')->join('zcshop_product_price', 'zcshop_product_price.itemid = zcshop_products.itemid', '*');
		$result=$db->fetchAll($select);
		return $result;
		
		
		
	}
}